package com.jye.rseven.user.controller;



import com.jye.rseven.domain.Classes;
import com.jye.rseven.domain.User;
import com.jye.rseven.service.ClassService;
import com.jye.rseven.domain.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@RestController
@RequestMapping("/class")
@Api(tags = "班级控制器API")
public class ClassController {

    @Autowired
    ClassService classService;

    /*
* 通过用户id查找我的班级
* */
    @GetMapping("/findMyClassesByUserId")
    @ApiOperation(value = "查找我的班级", notes = "根据用户id查找我的班级", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "userId",
                    value = "用户ID", required = true,
                    dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "skip",
                    value = "跳过多少条", required = true,
                    dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "limit",
                    value = "限制多少条", required = true,
                    dataType = "Integer"),

    })
    public JsonResult findMyClassesByUserId(int userId,int skip,int limit) {
        JsonResult jsonResult = new JsonResult();
        List<Classes> classesList = classService.findMyClassesByUserId(userId,skip,limit);
        if (!StringUtils.isEmpty(classesList)) {
            jsonResult.setData(classesList);
        } else {
            jsonResult.setCode("500");
            jsonResult.setMsg("查询失败");
        }
        return jsonResult;
    }

    /*
 * 通过id查找班级
 * */
    @GetMapping("/findClassById")
    @ApiOperation(value = "通过班级id查找班级", notes = "根据班级id查找我的班级", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id",
                    value = "课程ID", required = true,
                    dataType = "Integer"),
    })
    public JsonResult findClassById(int id) {
        JsonResult jsonResult = new JsonResult();
        Classes c = classService.findClassById(id);
        if (!StringUtils.isEmpty(c)) {
            jsonResult.setData(c);
        } else {
            jsonResult.setCode("500");
            jsonResult.setMsg("查询失败");
        }
        return jsonResult;
    }







    /*
    * 查找班级
    * */
    @GetMapping("/findAllClass")
    @ApiOperation(value = "查找所有班级", notes = "查找所有班级", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "skip",
                    value = "跳过多少条", required = true,
                    dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "limit",
                    value = "限制多少条", required = true,
                    dataType = "Integer"),
    })
    public JsonResult findAllClass(int skip, int limit) {
        JsonResult jsonResult = new JsonResult();
        List<Classes> list = classService.findAllClass(skip, limit);
        if (list != null) {
            jsonResult.setData(list);
        } else {
            jsonResult.setCode("500");
            jsonResult.setMsg("查询失败");
        }
        return jsonResult;
    }

    /*
   * 增加班级
   * */
    @PostMapping("/addClass")
    @ApiOperation(value = "添加班级", notes = "根据班级实体添加实体", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "classes",
                    value = "课程实体类", required = true),
    })
    public JsonResult addClass(Classes classes) {
        JsonResult jsonResult = new JsonResult();
        int i = classService.addClass(classes);
        if (i == 1) {
            jsonResult.setMsg("添加成功!");

        } else {
            jsonResult.setMsg("添加失败!");
            jsonResult.setCode("500");
        }
        return jsonResult;

    }

    /*
    * 修改班级信息
    * */
    @PutMapping("/updateClass")
    @ApiOperation(value = "修改班级信息", notes = "修改班级信息,只能修改名字与描述", httpMethod = "PUT")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "classes",
                    value = "课程实体类", required = true,
                    dataType = "com.jye.rseven.user.domain.Classes"),
    })
    public JsonResult updateClass(Classes classes) {
        JsonResult jsonResult = new JsonResult();
        System.out.println(classes);
        int i = classService.updateClass(classes);
        if (i == 1) {
            jsonResult.setMsg("修改成功!");
        } else {
            jsonResult.setMsg("修改失败!");
            jsonResult.setCode("500");
        }
        return jsonResult;
    }

    /*
    * 用户加入班级
    * 还没确定参数是实体类还是id
    * */
    @PostMapping("/addUserToClass")
    @ApiOperation(value = "用户加入班级", notes = "暂定参数为用户id和班级id", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "uId",
                    value = "用户id", required = true,
                    dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "cId",
                    value = "课程id", required = true,
                    dataType = "Integer"),
    })
    public JsonResult addUserToClass(int uId, int cId) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setMsg(classService.addUserToClass(uId, cId));
        return jsonResult;
    }


    @PostMapping("/addUsersToClass")
    @ApiOperation(value = "用户们加入班级", notes = "通过用户id数组加入班级", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "uIds",
                    value = "用户id", required = true,
                    dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "cId",
                    value = "课程id", required = true,
                    dataType = "Integer"),
    })
    public JsonResult addUsersToClass(int[] uIds, int cId) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setMsg(classService.addUsersToClass(uIds, cId));
        return jsonResult;
    }


    @GetMapping("/findClassUsersByCId")
    @ApiOperation(value = "通过课程id查找班级用户", notes = "通过cId(课程id)查找班级用户", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "cId",
                    value = "课程id", required = true,
                    dataType = "Integer"),
    })
    public JsonResult findClassUsersByCId(int cId) {
        JsonResult jsonResult = new JsonResult();
        List<User> users = classService.findClassUsersByCId(cId);
        if(users.size()!=0){
            jsonResult.setData(users);
        }else {
            jsonResult.setMsg("该班级无用户!");
        }
        return jsonResult;
    }


    @GetMapping("/findClassByCId")
    @ApiOperation(value = "通过课程id查找课程的分班", notes = "通过cId查找课程的分班", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "cId",
                    value = "课程id", required = true,
                    dataType = "Integer"),
    })
    public JsonResult findClassByCId(int cId) {
        JsonResult jsonResult = new JsonResult();
        List<Classes> classes = classService.findClassByCId(cId);
        if(classes.size()!=0){
            jsonResult.setData(classes);
        }else {
            jsonResult.setMsg("该班级无用户!");
        }
        return jsonResult;
    }


    @GetMapping("/findClassUserById")
    @ApiOperation(value = "通过班级id查找班级用户", notes = "通过班级id查找班级的用户", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id",
                    value = "班级id", required = true,
                    dataType = "Integer"),
    })
    public JsonResult findClassUserById(int id) {
        JsonResult jsonResult = new JsonResult();
        List<User> userList = classService.findClassUserById(id);
        if(userList.size()!=0){
            jsonResult.setData(userList);
        }else {
            jsonResult.setMsg("该班级无用户!");
        }
        return jsonResult;
    }

    @GetMapping("/findClassByShareCode")
    @ApiOperation(value = "分享码查找班级", notes = "通过分享码shareCode查找班级", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "shareCode",
                    value = "分享码", required = true,
                    dataType = "String"),
    })
    public JsonResult findClassByShareCode(String shareCode) {
        JsonResult jsonResult = new JsonResult();
        Classes classes = classService.findClassByShareCode(shareCode);
        if(!StringUtils.isEmpty(classes)){
            jsonResult.setData(classes);
        }else {
            jsonResult.setMsg("未查找到该班级!请确认分享码是否正确!");
        }
        return jsonResult;
    }


    @GetMapping("/findClassByName")
    @ApiOperation(value = "名字模糊查找班级", notes = "通过班级名字模糊查找班级", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "name",
                    value = "分享码", required = true,
                    dataType = "String"),
    })
    public JsonResult findClassByName(String name) {
        JsonResult jsonResult = new JsonResult();
        List<Classes> classList = classService.findClassByName(name);
        if(classList.size()!=0){
            jsonResult.setData(classList);
        }else {
            jsonResult.setMsg("未查找到["+name+"]班级!");
        }
        return jsonResult;
    }

    @DeleteMapping("/deleteClassById")
    @ApiOperation(value = "删除班级", notes = "通过班级id删除班级", httpMethod = "DELETE")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id",
                    value = "班级id", required = true,
                    dataType = "Integer"),
    })
    public JsonResult deleteClassById(int id) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setMsg(classService.deleteClassById(id));
        return jsonResult;
    }


    @DeleteMapping("/deleteUserToClassByUIdAndCId")
    @ApiOperation(value = "移除该用户出此班级", notes = "通过用户id,班级id移除", httpMethod = "DELETE")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "uId",
                    value = "用户id", required = true,
                    dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "cId",
                    value = "班级id", required = true,
                    dataType = "Integer"),
    })
    public JsonResult deleteUserToClassByUIdAndCId(int uId,int cId) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setMsg(classService.deleteUserToClassByUIdAndCId(uId,cId));
        return jsonResult;
    }


    @GetMapping("/findClassByUIdAndCId")
    @ApiOperation(value = "用户查找自己在该课程下班级", notes = "通过课程id,用户id来找用户在该课程下班级", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "uId",
                    value = "用户id", required = true,
                    dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "cId",
                    value = "课程id", required = true,
                    dataType = "Integer"),
    })
    public JsonResult findClassByUIdAndCId(int uId,int cId) {
        JsonResult jsonResult = new JsonResult();
        //找到该课程班级
        List<Classes> classesList = classService.findClassByCId(cId);
        int i = 0;
        for (Classes classes : classesList) {
            //是否在班级
            i = classService.isInClass(uId,classes.getId());
            if(i == 1){
                //在该班级
                jsonResult.setData(classes);
            }
        }
        if(jsonResult.getData()==null){
            jsonResult.setCode("500");
            jsonResult.setMsg("你未分班!请联系老师分班!");
        }

        return jsonResult;
    }


    @GetMapping("/findClassUserMonthsCreditById")
    @ApiOperation(value = "查询班级学生一个月内积分排行状况", notes = "通过班级id查询班级学生一个月内积分排行状况", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "cId",
                    value = "班级id", required = true,
                    dataType = "Integer"),
    })
    public JsonResult findClassUserMonthsCreditById(int id) {
        JsonResult jsonResult = new JsonResult();
        List<User> userList = classService.findClassUserMonthsCreditById(id);

        //算法
        Collections.sort(userList, new Comparator<User>() {
            @Override
            public int compare(User u1, User u2) {
                int t1 = u1.getCreditRecord().getChange();
                int t2 = u2.getCreditRecord().getChange();
                return t1 == t2 ? 0 : (t1 < t2 ? 1 : -1);
            }
        });
        if(userList.size()!=0){
            return new JsonResult("200","查询成功!",userList);
        }else{
            return new JsonResult("500","查询失败!",null);
        }
    }

    @GetMapping("/findClassUserCreditById")
    @ApiOperation(value = "查询班级学生积分排行状况", notes = "通过班级id查询班级学生积分排行状况", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "cId",
                    value = "班级id", required = true,
                    dataType = "Integer"),
    })
    public JsonResult findClassUserCreditById(int id) {
        JsonResult jsonResult = new JsonResult();
        List<User> userList = classService.findClassUserCreditById(id);
        //算法
        Collections.sort(userList, new Comparator<User>() {
            @Override
            public int compare(User u1, User u2) {
                Integer u1C = u1.getCredit().getCredit();
                Integer u2C = u2.getCredit().getCredit();
                return u1C == u2C ? 0 : (u1C < u2C ? 1 : -1);
            }
        });
        if(userList.size()!=0){
            return new JsonResult("200","查询成功!",userList);
        }else{
            return new JsonResult("500","查询失败!",null);
        }
    }





}
